package intelligentrecommender.phases.nlp;

import cue.lang.WordIterator;
import cue.lang.stop.StopWords;
import intelligentrecommender.phases.utilities.TagInfo;
import java.util.HashMap;
import java.util.Map;
import edu.stanford.nlp.process.Morphology;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;
import java.io.IOException;

/**
 *
 * @author Ahmad-Yaghi, Eias Tawil
 */
public class StopWordRemovalAndStemming {

    public String process(Map<String, TagInfo> data) {
        String dataStringResultTemp = "";
        String dataStringResult = "";
        Map<String, TagInfo> result = new HashMap<String, TagInfo>();
        String currentTagContent;
        Morphology morpha = new Morphology();

        for (Map.Entry<String, TagInfo> entry : data.entrySet()) {
            String key = (String) entry.getKey();
            TagInfo ti = (TagInfo) entry.getValue();
            currentTagContent = ti.getTagContent();
            String tagWithoutStopwords = "";
            currentTagContent = currentTagContent.replaceAll("http.*?\\s", " ");
            currentTagContent = currentTagContent.replaceAll("\\W+", " ");
            for (String word : new WordIterator(currentTagContent)) {
                //word = word.replaceAll("http.*?\\s", " ");
                //word = word.replaceAll("[ \t\n,\\.\"!?$~()\\[\\]\\{\\}:;/\\\\<>+=%*]", " ");
                word = morpha.stem(word);
                // Remove stop words and numbers
                //if (!StopWords.English.isStopWord(word) || !word.matches("-?\\d+(\\.\\d+)?") || (word.length() > 3)) {
                if (!StopWords.English.isStopWord(word)) {
                    dataStringResultTemp += word + " ";
                    //System.out.println(word);
                    //tagWithoutStopwords += word + " ";
                }
            }
            result.put(key, new TagInfo(ti.getUrl(), ti.getTagName(), tagWithoutStopwords, ti.getOriginalTagContent()));
        }

        try {
            MaxentTagger tagger = new MaxentTagger("src/intelligentrecommender/phases/utilities/models/wsj-0-18-left3words.tagger");
            for (String word : tagger.tagString(dataStringResultTemp).split(" ")) {
                if (!(word.contains("_C") || word.contains("_V"))) {
                    int spaceIndex = word.indexOf("_");
                    if (spaceIndex != -1) {
                        word = word.substring(0, spaceIndex);
                    }
                    dataStringResult += word + " ";
                }
            }
        } catch (IOException | ClassNotFoundException ex) {
        }

        /*String[] options = {"-maxLength", "80", "-retainTmpSubcategories"};
         LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz", options);
         Tree parse = (Tree) lp.apply(dataStringResultTemp); // finally, we actually get to parse something
         for (TaggedWord tw : parse.taggedYield()) {
         if (!(tw.tag().startsWith("V") || tw.tag().startsWith("C"))) {
         System.out.println(tw.word());
         dataStringResult += tw.word() + " ";
         }
         }*/

        return dataStringResult;
    }

    /*public static void main(String[] args) {
     //TODO code application logic here
     Morphology morpha = new Morphology();
     String old = "his Money is the most important and essential tool of modern economic life working. One cannot imagine modern economic life without money. Barter economies or self-sufficient economies are the thing of past and historical significance. Money is the foundation stone of modern economic life. Money is the pivot around which all economic activities revolve. Money is important in all economies whether it is a socialist economy or a capitalist economy; or a backward economy or a rich economy. In the initial stages of exchange economy, commodities were exchanged with commodities. Such transactions were known as barter transactions. In this stage commodity was the form of money. How­ever, under barter transactions there were difficulties relating to common measure of value, store of value, divisibility of goods etc. Therefore, in place of commodities gold and silver were used as money. But even this system could not continue for long because the available stock of gold and silver was not adequate to meet the demand for the quantity of money. Therefore, it was replaced by paper money where promissory notes arc used as money. Now, besides paper money, credit money is being used exten­sively such as credit cards etc. There are many functions of money such as medium of exchange, basis of store of value, common measure of value, standard of deferred payments etc. However, money as a medium of exchange is the most important function. Therefore, in general, by money we mean such commodities which are ac­cepted as medium of exchange and which have legal sanction and general acceptability.";
     for (String word : new WordIterator(old)) {

     if (!StopWords.English.isStopWord(word)) {
     word = morpha.stem(word);
     System.out.println(word);
     }
     }
     }*/
}
/*        public Map<String, List<TagInfo>> stopWordRemoval(String st, List<TagInfo> taginfo) {
 Map<String, List<TagInfo>> result = new HashMap<>();
 List<TagInfo> tagInfo = new ArrayList<>();
 for (final String word : new WordIterator(st)) {
 if (!StopWords.English.isStopWord(word)) {
 result.put(word, tagInfo);
 System.out.println("" + word);
 }
 }
 return result;
 }
 }*/

/*public Map<String, List<TagInfo>> process1(Map<String, List<TagInfo>> data) {

     Map<String, List<TagInfo>> result = new HashMap<String, List<TagInfo>>();
     List<TagInfo> list = new ArrayList<TagInfo>();
     String url = null, tagContent, tagName, orginalTagContent;
     for (int i = 0; i < data.size(); i++) {
     for (int j = 0; j < data.get(i).size(); j++) {
     url = data.get(i).get(j).getUrl();
     tagName = data.get(i).get(j).getTagName();
     orginalTagContent = data.get(i).get(j).getOriginalTagContent();
     tagContent = data.get(i).get(j).getTagContent();
     String tagWithoutStopwords = "";
     for (final String word : new WordIterator(tagContent)) {
     if (!StopWords.English.isStopWord(word)) {
     tagWithoutStopwords += word;
     }
     }
     list.add(new TagInfo(url, tagName, tagWithoutStopwords, orginalTagContent));
     }
     result.put(url, list);
     }
     return result;
     }*/